#!/usr/bin/env python
import gtk, gobject
import pysqlite2
import urllib2
import dieu.settings, dieu.util

class DieuPreferencesWindow:
    def __init__(self, Title='dieu Preferences', firststart=False):
        self.preferences_window = gtk.Window()
        self.preferences_window.set_title(Title)
        self.preferences_window.set_icon(gtk.gdk.pixbuf_new_from_file('@prefix@/share/pixmaps/dieu/dieu.svg'))
        self.preferences_window.set_border_width(12)
        
        self.preferences_vbox = gtk.VBox()
        self.preferences_notebook = gtk.Notebook()
        self.preferences_window.add(self.preferences_vbox)
        
        self.general_page()
        self.accounts_page()
        
        self.preferences_notebook_alignment = gtk.Alignment()
        self.preferences_notebook_alignment.set_padding(0, 6, 0, 0)
        self.preferences_notebook_alignment.add(self.preferences_notebook)
        self.preferences_vbox.pack_start(self.preferences_notebook_alignment)
        
        self.preferences_buttonbox = gtk.HButtonBox()
        self.preferences_buttonbox.set_layout(gtk.BUTTONBOX_END)
        self.preferences_vbox.pack_start(self.preferences_buttonbox)
        
        self.preferences_buttons_close = gtk.Button(stock=gtk.STOCK_CLOSE)
        self.preferences_buttons_close.connect('clicked', lambda w: self.preferences_window.destroy())
        self.preferences_buttonbox.add(self.preferences_buttons_close)
        
        self.preferences_firststart_dialog = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO, buttons=gtk.BUTTONS_NONE, message_format='Welcome to dieu. Before you start posting, you must add at least one account to dieu.')
        self.preferences_firststart_dialog.set_title('Welcome to dieu.')
        if firststart == True:
            self.preferences_firststart_dialog.show()
        
        self.preferences_buttons_close.show()
        self.accounts_remove_button.show()
        self.accounts_add_button.show()
        self.accounts_hbox.show()
        self.accounts_treeview.show()
        self.accounts_label.show()
        self.accounts_vbox.show()
        self.general_alignment_Allposts.show()
        self.general_check_Allposts.show()
        self.general_alignment_Recent.show()
        self.general_check_Recent.show()
        self.general_label_Tray.show()
        self.general_alignment_Gstreamer.show()
        self.general_check_Gstreamer.show()
        self.general_alignment_Libnotify.show()
        self.general_check_Libnotify.show()
        self.general_alignment_Avahi.show()
        self.general_check_Avahi.show()
        self.general_label_General.show()
        self.general_table.show()
        self.general_label.show()
        self.general_vbox.show()
        self.preferences_notebook.show()
        self.preferences_buttonbox.show()
        self.preferences_notebook_alignment.show()
        self.preferences_vbox.show()
        self.preferences_window.show()
        
    def general_page(self):
        self.general_vbox = gtk.VBox(False, 0)
        self.general_label = gtk.Label('General')
        self.general_table = gtk.Table(1, 8)
        self.general_table.set_row_spacings(3)
        self.general_vbox.pack_start(self.general_table)
        
        self.general_label_General = gtk.Label('General')
        self.general_label_General.set_markup('<b>General</b>')
        self.general_label_General.set_alignment(0.0, 0.5)
        self.general_table.attach(self.general_label_General, 0, 1, 0, 1, gtk.FILL, gtk.FILL)
        
        self.general_check_Avahi = gtk.CheckButton('Find other users with Avahi')
        self.general_alignment_Avahi = gtk.Alignment()
        self.general_alignment_Avahi.set_padding(0, 0, 6, 6)
        self.general_alignment_Avahi.add(self.general_check_Avahi)
        self.general_check_Avahi.set_sensitive(False)
        self.general_table.attach(self.general_alignment_Avahi, 0, 1, 1, 2, gtk.FILL, gtk.FILL)
        
        self.general_check_Libnotify = gtk.CheckButton('Display notifications on completion')
        self.general_alignment_Libnotify = gtk.Alignment()
        self.general_alignment_Libnotify.set_padding(0, 0, 6, 6)
        self.general_alignment_Libnotify.add(self.general_check_Libnotify)
        self.general_check_Libnotify.connect('clicked', lambda w: self.libnotify_call())
        self.general_table.attach(self.general_alignment_Libnotify, 0, 1, 2, 3, gtk.FILL, gtk.FILL)
        
        if dieu.settings.get_libnotify() == True:
            self.general_check_Libnotify.set_active(True)
        
        self.general_check_Gstreamer = gtk.CheckButton('Play annoying sounds')
        self.general_alignment_Gstreamer = gtk.Alignment()
        self.general_alignment_Gstreamer.set_padding(0, 0, 6, 6)
        self.general_alignment_Gstreamer.add(self.general_check_Gstreamer)
        self.general_check_Gstreamer.set_sensitive(False)
        self.general_table.attach(self.general_alignment_Gstreamer, 0, 1, 3, 4, gtk.FILL, gtk.FILL)
        
        self.general_label_Tray = gtk.Label('Status Icon')
        self.general_label_Tray.set_markup('<b>Status Icon</b>')
        self.general_label_Tray.set_alignment(0.0, 0.5)
        self.general_table.attach(self.general_label_Tray, 0, 1, 4, 5, gtk.FILL, gtk.FILL)
        
        self.general_check_Recent = gtk.CheckButton('Show recent posts')
        self.general_alignment_Recent = gtk.Alignment()
        self.general_alignment_Recent.set_padding(0, 0, 6, 6)
        self.general_alignment_Recent.add(self.general_check_Recent)
        self.general_check_Recent.connect('clicked', lambda w: self.recent_call())
        self.general_table.attach(self.general_alignment_Recent, 0, 1, 5, 6, gtk.FILL, gtk.FILL)
        
        if dieu.settings.get_recent() == True:
            self.general_check_Recent.set_active(True)
        
        self.general_check_Allposts = gtk.CheckButton('Show posts sorted by tag and user')
        self.general_alignment_Allposts = gtk.Alignment()
        self.general_alignment_Allposts.set_padding(0, 0, 6, 6)
        self.general_alignment_Allposts.add(self.general_check_Allposts)
        self.general_check_Allposts.connect('clicked', lambda w: self.allposts_call())
        self.general_table.attach(self.general_alignment_Allposts, 0, 1, 6, 7, gtk.FILL, gtk.FILL)
        
        if dieu.settings.get_show_all() == True:
            self.general_check_Allposts.set_active(True)
        
        self.preferences_notebook.append_page(self.general_vbox, tab_label=self.general_label)
    
    def accounts_page(self):
        self.accounts_vbox = gtk.Table(1, 2)
        self.accounts_label = gtk.Label('Accounts')
        self.preferences_notebook.append_page(self.accounts_vbox, tab_label=self.accounts_label)
        
        self.accounts_treeview_model = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
        self.accounts_treeview = gtk.TreeView(self.accounts_treeview_model)
        self.accounts_treeview_renderer = gtk.CellRendererText()
        self.accounts_treeview_column = gtk.TreeViewColumn('Account', self.accounts_treeview_renderer, text=1)
        self.accounts_treeview.append_column(self.accounts_treeview_column)
        self.accounts_treeview.set_size_request(200, 250)
        
        for user in dieu.settings.get_users():
            self.accounts_treeview_model.append([gobject.TYPE_STRING, user])
        
        self.accounts_vbox.attach(self.accounts_treeview, 0, 1, 0, 1)
        
        self.accounts_hbox = gtk.HBox()
        
        self.accounts_add_button = gtk.Button(stock=gtk.STOCK_ADD)
        self.accounts_hbox.pack_start(self.accounts_add_button)
        self.accounts_add_button.connect('clicked', lambda w: DieuNewAccount(self))
        
        self.accounts_remove_button = gtk.Button(stock=gtk.STOCK_REMOVE)
        self.accounts_remove_button.connect('clicked', lambda w: self.remove_call())
        self.accounts_hbox.pack_start(self.accounts_remove_button)
        
        self.accounts_vbox.attach(self.accounts_hbox, 0, 1, 1, 2, gtk.FILL)
        

    def libnotify_call(self):
        if self.general_check_Libnotify.get_active() == True:
            dieu.settings.set_libnotify(True)
        else:
            dieu.settings.set_libnotify(False)
    
    def recent_call(self):
        if self.general_check_Recent.get_active() == True:
            dieu.settings.set_recent(True)
        else:
            dieu.settings.set_recent(False)
    
    def allposts_call(self):
        if self.general_check_Allposts.get_active() == True:
            dieu.settings.set_show_all(True)
        else:
            dieu.settings.set_show_all(False)
    
    def remove_call(self):
        selection = self.accounts_treeview.get_selection()
        selection2 = selection.get_selected()
        selectionstore = selection2[0]
        selectioniter = selection2[1]
        try:
            if selectionstore.get_value(selectioniter, 1) in dieu.settings.get_users():
                userslist = dieu.settings.get_users()
                userslist.remove(selectionstore.get_value(selectioniter, 1))
                import gconf
                __gconf_client = gconf.client_get_default()
                __gconf_client.set_list('/apps/dieu-ng/usernames', 'string', userslist)
                self.accounts_treeview.destroy()
                self.accounts_treeview_model = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
                self.accounts_treeview = gtk.TreeView(self.accounts_treeview_model)
                self.accounts_treeview_renderer = gtk.CellRendererText()
                self.accounts_treeview_column = gtk.TreeViewColumn('Account', self.accounts_treeview_renderer, text=1)
                self.accounts_treeview.append_column(self.accounts_treeview_column)
                self.accounts_treeview.set_size_request(200, 250)
                
                for user in dieu.settings.get_users():
                    self.accounts_treeview_model.append([gobject.TYPE_STRING, user])
                
                self.accounts_vbox.attach(self.accounts_treeview, 0, 1, 0, 1)
                self.accounts_treeview.show()
        except TypeError:
            pass

class DieuNewAccount:
    def __init__(self, preferences=None):
        self.new_account()
        self.prefwin = preferences
        
    def new_account(self):
        self.new_window = gtk.Window()
        self.new_window.set_title('New account')
        self.new_window.set_icon_name('user-info')
        self.new_window.set_position(gtk.WIN_POS_CENTER)
        self.new_window.connect("delete_event", lambda w,e: self.new_window.destroy())
        self.new_window.set_border_width(12)
        self.new_window.set_resizable(False)
        
        self.new_table = gtk.Table(2, 4)
        self.new_table.set_row_spacings(6)
        
        self.new_labels_Username = gtk.Label('Username:')
        self.new_labels_Username.set_alignment(1.0, 0.5)
        self.new_table.attach(self.new_labels_Username, 0, 1, 0, 1, gtk.FILL, xpadding=6)
        
        self.new_entries_Username = gtk.Entry()
        self.new_table.attach(self.new_entries_Username, 1, 2, 0, 1)
        
        self.new_labels_Password = gtk.Label('Password:')
        self.new_labels_Password.set_alignment(1.0, 0.5)
        self.new_table.attach(self.new_labels_Password, 0, 1, 1, 2, gtk.FILL, xpadding=6)
        
        self.new_entries_Password = gtk.Entry()
        self.new_entries_Password.set_visibility(False)
        self.new_entries_Password.set_invisible_char(u"\u25CF")
        self.new_table.attach(self.new_entries_Password, 1, 2, 1, 2)
        
        self.new_labels_System = gtk.Label('System:')
        self.new_labels_System.set_alignment(1.0, 0.5)
        self.new_table.attach(self.new_labels_System, 0, 1, 2, 3, gtk.FILL, xpadding=6)
        
        self.new_combobox_System_liststore = gtk.ListStore(gobject.TYPE_STRING)
        self.new_combobox_System = gtk.ComboBox(self.new_combobox_System_liststore)
        self.new_combobox_System_cell = gtk.CellRendererText()
        self.new_combobox_System.pack_start(self.new_combobox_System_cell, True)
        self.new_combobox_System.add_attribute(self.new_combobox_System_cell, 'text', 0)
        self.new_combobox_System.append_text('del.icio.us')
        self.new_combobox_System.append_text('ma.gnolia')
        self.new_combobox_System.append_text('bluedot')
        self.new_table.attach(self.new_combobox_System, 1, 2, 2, 3)
        
        self.new_buttonhbox = gtk.HButtonBox()
        self.new_buttonhbox.set_layout(gtk.BUTTONBOX_END)
        self.new_buttonhbox.set_spacing(6)
        self.new_table.attach(self.new_buttonhbox, 0, 2, 3, 4)
        
        self.new_buttons_cancel = gtk.Button(stock=gtk.STOCK_CANCEL)
        self.new_buttons_cancel.connect('clicked', lambda w: self.new_window.destroy())
        self.new_buttonhbox.add(self.new_buttons_cancel)
        
        self.new_buttons_add = gtk.Button(stock=gtk.STOCK_ADD)
        self.new_buttons_add.connect('clicked', lambda w: self.new_account_call())
        self.new_buttonhbox.add(self.new_buttons_add)
        
        self.new_window.add(self.new_table)
        
        self.new_buttons_add.show()
        self.new_buttons_cancel.show()
        self.new_buttonhbox.show()
        self.new_combobox_System.show()
        self.new_labels_System.show()
        self.new_entries_Password.show()
        self.new_labels_Password.show()
        self.new_entries_Username.show()
        self.new_labels_Username.show()
        self.new_table.show()
        self.new_window.show()
        
    @dieu.util.threaded
    def new_account_call(self):
        
        gtk.gdk.threads_enter()
        self.new_window.hide()
        gtk.gdk.threads_leave()
    
        username = self.new_entries_Username.get_text()
        password = self.new_entries_Password.get_text()
        system = self.new_combobox_System.get_active_text()
        
        dieu.settings.create_user(username, password, system)
        
        if dieu.settings.get_default_user() == None:
            dieu.settings.set_default_user(username, system)
        
        gtk.gdk.threads_enter()
        if self.prefwin != None:
            self.prefwin.accounts_treeview.destroy()
            self.prefwin.accounts_treeview_model = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
            self.prefwin.accounts_treeview = gtk.TreeView(self.prefwin.accounts_treeview_model)
            self.prefwin.accounts_treeview_renderer = gtk.CellRendererText()
            self.prefwin.accounts_treeview_column = gtk.TreeViewColumn('Account', self.prefwin.accounts_treeview_renderer, text=1)
            self.prefwin.accounts_treeview.append_column(self.prefwin.accounts_treeview_column)
            self.prefwin.accounts_treeview.set_size_request(200, 250)
            
            for user in dieu.settings.get_users():
                self.prefwin.accounts_treeview_model.append([gobject.TYPE_STRING, user])
            
            self.prefwin.accounts_vbox.attach(self.prefwin.accounts_treeview, 0, 1, 0, 1)
            self.prefwin.accounts_treeview.show()
            
            self.prefwin.preferences_window.set_sensitive(False)
        gtk.gdk.threads_leave()
        
        try: dieu.util.sync()
        except urllib2.HTTPError:
            pass
            # We should have a pretty dialog saying 'omg your account settings are wrong!!!' but we don't right now
            
        
        gtk.gdk.threads_enter()
        self.prefwin.preferences_window.set_sensitive(True)
        gtk.gdk.threads_leave()
        
        self.new_window.destroy()
